package xyz.renyumeng.xkm.shop.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import xyz.renyumeng.xkm.dto.base.SearchPage;
import xyz.renyumeng.xkm.dto.shop.ProductInfoDto;
import xyz.renyumeng.xkm.model.es.EsProduct;
import xyz.renyumeng.xkm.result.Result;
import xyz.renyumeng.xkm.shop.service.UserProductService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author renyumeng
 * @date Created in 10:39 2023/7/2
 */

@RestController
@RequestMapping("/user/shop/product")
@Api(tags = "用户商品信息接口")
public class UserProductController {

    @Resource
    private UserProductService userProductService;


    @PutMapping("/{productId}")
    @ApiOperation(value = "上下架商品信息(0上架1下架)")
    public Result<Object> listedProduct(@PathVariable Long productId, @RequestParam("isListed") Integer isListed) {
        return userProductService.listedProduct(productId, isListed);
    }

    @GetMapping("/search/{current}/{size}")
    @ApiOperation(value = "分页查询所有商品信息(未售出且未下架)(filter:商品名称，详情信息)")
    public Result<SearchPage<List<EsProduct>>> pageProductIInfoWithFilter(@PathVariable Integer current,
                                                                          @PathVariable Integer size,
                                                                          @RequestParam(value = "filter", required = false) String filter) {
        return userProductService.pageProductIInfoWithFilter(current, size, filter);
    }

    @GetMapping("/search/{productId}")
    @ApiOperation(value = "根据id查询一个商品的信息")
    public Result<EsProduct> getProductById(@PathVariable Long productId) {
        return userProductService.getProductById(productId);
    }

    @GetMapping("/owner/{page}/{size}/{ownerId}")
    @ApiOperation(value = "根据id分页查询用户发布的所有商品")
    public Result<SearchPage<List<EsProduct>>> getProductListByOwnerId(@PathVariable Long ownerId,
                                                                       @PathVariable Integer page,
                                                                       @PathVariable Integer size) {
        return userProductService.getProductListByOwnerId(ownerId,page,size);
    }

    @GetMapping("/owner/isSole/{page}/{size}/{ownerId}")
    @ApiOperation(value = "根据用户id查询用户已经售出的商品")
    public Result<SearchPage<List<EsProduct>>> getProductIsSoleListByOwnerId(@PathVariable Long ownerId,
                                                                             @PathVariable Integer page,
                                                                             @PathVariable Integer size){
        return userProductService.getProductIsSoleListByOwnerId(ownerId,page,size);
    }

    @PostMapping
    @ApiOperation(value = "新增一个商品信息")
    public Result<Object> addProduct(@RequestBody ProductInfoDto productInfoDto) {
        return userProductService.addProductInfo(productInfoDto);
    }

    @PutMapping
    @ApiOperation(value = "修改一个商品信息")
    public Result<Object> updateProduct(@RequestBody ProductInfoDto productInfoDto) {
        return userProductService.upDateProduct(productInfoDto);
    }

    @DeleteMapping("/{productId}")
    @ApiOperation(value = "删除一个商品信息")
    public Result<Object> deleteProduct(@PathVariable Long productId) {
        return userProductService.deleteProductById(productId);
    }


}
